package com.jeasonzhao.report.vml.contrib;

import java.text.DecimalFormat;

import com.jeasonzhao.report.vml.base.Color;
import com.jeasonzhao.report.vml.base.LineText;
import com.jeasonzhao.report.vml.base.Vector2D;
import com.jeasonzhao.report.vml.chart.Label;
import com.jeasonzhao.report.vml.chart.Legend;
import com.jeasonzhao.report.vml.chart.LegendCollection;

public class TextHelper
{
    public static int getPixelLength(String str,int nFontWidth)
    {
        return nFontWidth * (str == null ? 0 : str.getBytes().length);
    }

    public static int getMaxPixelLength(int nBytePixel,String[] argvs)
    {
        int nMax = 0;
        if(null == argvs)
        {
            return 0;
        }
        for(int nx = 0;null != argvs && nx < argvs.length;nx++)
        {
            String str = argvs[nx];
            if(str == null)
            {
                continue;
            }
            int n = getPixelLength(str,nBytePixel);
            nMax = n > nMax ? n : nMax;
        }
        return nMax;
    }

    public static int getMaxPixelLength(int nBytePixel,Label[] arg)
    {
        int nMax = 0;
        if(null == arg)
        {
            return 0;
        }
        for(int x = 0;null != arg && x < arg.length;x++)
        {
            Label la = arg[x];
            if(la == null || la.getTitle() == null)
            {
                continue;
            }
            int n = getPixelLength(la.getTitle(),nBytePixel);
            nMax = n > nMax ? n : nMax;
        }
        return nMax;
    }

    public static int getMaxPixelLength(int nBytePixel,LegendCollection arg)
    {
        int nMax = 0;
        if(null == arg)
        {
            return 0;
        }
        for(int nx = 0;null != arg && nx < arg.size();nx++)
        {
            Legend la = (Legend) arg.get(nx);
            if(la == null || la.getTitle() == null)
            {
                continue;
            }
            int n = getPixelLength(la.getTitle(),nBytePixel);
            nMax = n > nMax ? n : nMax;
        }
        return nMax;
    }

    public static LineText getHorizonTextLine(String str,Vector2D pt,int nLength)
    {
        return getLineText(str,new Vector2D(pt),new Vector2D(pt.getX() + nLength,pt.getY() - 1));
    }

    public static LineText bevelByTop(Vector2D pt,String str,int nFontWidth,int nZindex,Color clr)
    {
        int nLength = getPixelLength(str,nFontWidth);
        Vector2D pt2 = new Vector2D(pt);
        nLength = (int) (nLength * Math.sqrt(2) / 2);
        pt2.offset( -nLength,nLength);
        return getLineText(str,new Vector2D(pt2),new Vector2D(pt),nZindex,clr);
    }

    public static LineText getVerticalTextLine(String str,Vector2D pt,int nLength)
    {
        return getLineText(str,new Vector2D(pt),new Vector2D(pt.getX() - 1,pt.getY() + nLength));
    }

    public static LineText getLineText(String str,Vector2D ptBegin,Vector2D ptEnd)
    {
        return getLineText(str,ptBegin,ptEnd,0,null);
    }

    public static LineText getLineText(String str,Vector2D ptBegin,Vector2D ptEnd,int zIndex,Color clr)
    {
        LineText text = new LineText();
        text.setText(str);
        text.setFrom(ptBegin.getX(),ptBegin.getY());
        text.setTo(ptEnd.getX(),ptEnd.getY());
        if(ptBegin.getX() == ptEnd.getX())
        {
            text.setTo(ptEnd.getX() - 1,ptEnd.getY());
        }
        else if(ptBegin.getY() == ptEnd.getY())
        {
            text.setTo(ptEnd.getX(),ptEnd.getY() - 1);
        }
        text.setZIndex(zIndex);
        text.setStrokeColor(clr);
        return text;
    }

    public static LineText horizonByCenter(Vector2D ptCenter,String strText,int nFontWidth)
    {
        int nTextLength = getPixelLength(strText,nFontWidth);
        if(nTextLength <= 0)
        {
            return null;
        }
        return getLineText(strText,ptCenter.offsetCopy(0 - nTextLength / 2,0),ptCenter.offsetCopy(nTextLength / 2,0));
    }

    public static LineText horizonByTop(Vector2D ptCenter,String strText,int nFontWidth,int zIndex,Color clr)
    {
        int nTextLength = getPixelLength(strText,nFontWidth);
        if(nTextLength <= 0)
        {
            return null;
        }
        return getLineText(strText,ptCenter.offsetCopy(0 - nTextLength / 2,nFontWidth / 2),
                           ptCenter.offsetCopy(nTextLength / 2,nFontWidth / 2),zIndex,clr);
    }

    public static LineText horizonByBottom(Vector2D ptCenter,String strText,int nFontWidth)
    {
        int nTextLength = getPixelLength(strText,nFontWidth);
        if(nTextLength <= 0)
        {
            return null;
        }
        return getLineText(strText,ptCenter.offsetCopy(0 - nTextLength / 2, -nFontWidth / 2),
                           ptCenter.offsetCopy(nTextLength / 2, -nFontWidth / 2));
    }

    public static LineText horizonByRight(Vector2D ptCenter,String strText,int nFontWidth,int zIndex,Color clr)
    {
        int nTextLength = getPixelLength(strText,nFontWidth);
        if(nTextLength <= 0)
        {
            return null;
        }
        return getLineText(strText,ptCenter.offsetCopy(0 - nTextLength,0),ptCenter.offsetCopy(0,0),zIndex,clr);
    }

    public static LineText horizonByLeft(Vector2D ptCenter,String strText,int nFontWidth,int zIndex,Color clr)
    {
        int nTextLength = getPixelLength(strText,nFontWidth);
        if(nTextLength <= 0)
        {
            return null;
        }
        return getLineText(strText,ptCenter.offsetCopy(0,0),ptCenter.offsetCopy(nTextLength,0),zIndex,clr);
    }

    public static LineText verticalByCenter(Vector2D ptCenter,String strText,int nFontWidth)
    {
        int nTextLength = getPixelLength(strText,nFontWidth);
        if(nTextLength <= 0)
        {
            return null;
        }
        return getLineText(strText,
                           ptCenter.offsetCopy(0,0 - nTextLength / 2),
                           ptCenter.offsetCopy(0,nTextLength / 2));
    }

    public static LineText verticalByBottom(Vector2D ptCenter,String strText,int nFontWidth)
    {
        int nTextLength = getPixelLength(strText,nFontWidth);
        if(nTextLength <= 0)
        {
            return null;
        }
        return getLineText(strText,
                           ptCenter.offsetCopy(0,0 - nTextLength),
                           ptCenter.offsetCopy(0,0));
    }

    public static LineText verticalByTop(Vector2D ptCenter,String strText,int nFontWidth)
    {
        return verticalByTop(ptCenter,strText,nFontWidth,0,null);
    }

    public static LineText verticalByTop(Vector2D ptCenter,String strText,int nFontWidth,int zIndex,Color clr)
    {
        int nTextLength = getPixelLength(strText,nFontWidth);
        if(nTextLength <= 0)
        {
            return null;
        }
        return getLineText(strText,
                           ptCenter.offsetCopy(0,0),
                           ptCenter.offsetCopy(0,nTextLength),zIndex,clr);
    }

    private TextHelper()
    {
        super();
    }

    public static int getMaxPixelLength(int FontWidth,double[] ds,DecimalFormat df)
    {
        String[] arys = new String[ds.length];
        for(int n = 0;n < ds.length;n++)
        {
            arys[n] = df.format(ds[n]);
        }
        return getMaxPixelLength(FontWidth,arys);
    }

}
